package com.houweishi.module.train.controller;

import com.houweishi.common.annotation.Log;
import com.houweishi.common.core.controller.BaseController;
import com.houweishi.common.core.domain.AjaxResult;
import com.houweishi.common.core.page.TableDataInfo;
import com.houweishi.common.enums.BusinessType;
import com.houweishi.common.utils.poi.ExcelUtil;
import com.houweishi.module.train.domain.OaTrain;
import com.houweishi.module.train.domain.OaTrainApproval;
import com.houweishi.module.train.service.IOaTrainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 培训管理Controller
 * 
 * @author houweishi
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/module/train")
public class OaTrainController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(OaTrainController.class);
    @Autowired
    private IOaTrainService oaTrainService;

    /**
     * 查询培训管理列表
     */
    @PreAuthorize("@ss.hasPermi('module:train:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaTrain oaTrain)
    {
        startPage();
        List<OaTrainApproval> list = oaTrainService.selectOaTrainApprovalList(oaTrain);
        return getDataTable(list);
    }

    /**
     * 导出培训管理列表
     */
    @PreAuthorize("@ss.hasPermi('module:train:export')")
    @Log(title = "培训管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaTrain oaTrain)
    {
        List<OaTrain> list = oaTrainService.selectOaTrainList(oaTrain);
        ExcelUtil<OaTrain> util = new ExcelUtil<OaTrain>(OaTrain.class);
        util.exportExcel(response, list, "培训管理数据");
    }

    /**
     * 获取培训管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:train:query')")
    @GetMapping(value = "/{trainId}")
    public AjaxResult getInfo(@PathVariable("trainId") Long trainId)
    {
        return success(oaTrainService.selectOaTrainByTrainId(trainId));
    }

    /**
     * 新增培训管理
     */
    @PreAuthorize("@ss.hasPermi('module:train:add')")
    @Log(title = "培训管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaTrain oaTrain)
    {
        try {
            return oaTrainService.insertOaTrain(oaTrain);
        } catch (Exception e) {
            LOGGER.error("新增培训管理失败",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改培训管理
     */
    @PreAuthorize("@ss.hasPermi('module:train:edit')")
    @Log(title = "培训管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaTrain oaTrain)
    {
        return toAjax(oaTrainService.updateOaTrain(oaTrain));
    }

    /**
     * 删除培训管理
     */
    @PreAuthorize("@ss.hasPermi('module:train:remove')")
    @Log(title = "培训管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trainIds}")
    public AjaxResult remove(@PathVariable Long[] trainIds)
    {
        return toAjax(oaTrainService.deleteOaTrainByTrainIds(trainIds));
    }
}
